package com.eorchis.module.trainingquestionnaire.ui.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.eorchis.core.service.IBaseService;
import com.eorchis.core.springext.mvc.SessionConstant;
import com.eorchis.core.ui.commond.ResultState;
import com.eorchis.core.ui.controller.AbstractBaseController;
import com.eorchis.module.classuser.domain.QueryClassUserBean;
import com.eorchis.module.classuser.service.IClassUserService;
import com.eorchis.module.trainingneeds.querybean.ObjectiveItems;
import com.eorchis.module.trainingneeds.querybean.PageInfo;
import com.eorchis.module.trainingquestionnaire.domain.MCQStatisticsBean;
import com.eorchis.module.trainingquestionnaire.domain.QuestionsStatisticsBean;
import com.eorchis.module.trainingquestionnaire.domain.RadioStatisticsBean;
import com.eorchis.module.trainingquestionnaire.domain.ScoreStatisticsBean;
import com.eorchis.module.trainingquestionnaire.domain.StatisticsHeaderBean;
import com.eorchis.module.trainingquestionnaire.domain.json.StudentAnswerSelect;
import com.eorchis.module.trainingquestionnaire.domain.json.StudentPageInfoUtils;
import com.eorchis.module.trainingquestionnaire.service.IUserAnswerLinkService;
import com.eorchis.module.trainingquestionnaire.ui.commond.UserAnswerLinkQueryCommond;
import com.eorchis.module.trainingquestionnaire.ui.commond.UserAnswerLinkValidCommond;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: UserAnswerLinkController<br>
 * Description: 用户答卷关联信息管理控制器<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Max
 * @createDate 2014-08-28
 * @version $Revision$
 */
@Controller("userAnswerLinkController")
@RequestMapping(value = UserAnswerLinkController.MODULE_PATH)
public class UserAnswerLinkController 
		extends AbstractBaseController<UserAnswerLinkValidCommond,UserAnswerLinkQueryCommond> 
{
	// 模块页面根路径
	public static final String MODULE_PATH = "/module/useranswerlink";
	// 用户答卷关联信息接口
	@Autowired
	@Qualifier("com.eorchis.module.trainingquestionnaire.service.impl.UserAnswerLinkServiceImpl")
	private IUserAnswerLinkService userAnswerLinkService;
	
	// 培训班用户关系接口
	@Resource(name = "com.eorchis.module.classuser.service.impl.ClassUserServiceImpl")
	private IClassUserService classUserService;
	
	public IBaseService getService() {
		return userAnswerLinkService;
	}

	public String getModulePath() {
		return MODULE_PATH;
	}
	
	/**
	 * 统计某一个问卷下所有的主观题
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-08-28
	 */
	@RequestMapping(value = "/findAllSubjectivesByQnId")
	public String findAllSubjectivesByQnId(
			@ModelAttribute("result") UserAnswerLinkQueryCommond qCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState
			) throws Exception {
		qCommond.setResultList(userAnswerLinkService.findAllSubjectivesByQnId(qCommond));
		resultState.setMessage("查询成功");
		return "";
	}
	
	/**
	 * 统计某一个问卷下所有的主观题的答案
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-08-28
	 */
	@RequestMapping(value = "/findAllSubjectivesAnByQId")
	public String findAllSubjectivesAnByQId(
			@ModelAttribute("result") UserAnswerLinkQueryCommond qCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState
			) throws Exception {
		qCommond.setResultList(userAnswerLinkService.findAllSubjectivesAnByQId(qCommond));
		resultState.setMessage("查询成功");
		return "";
	}
	
	
	/**
	 * 统计---某一个问卷下所有的客观题
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-09-01
	 */
	@RequestMapping(value = "/findAllObjectiveByQnId")
	public String findAllObjectiveByQnId(
			@ModelAttribute("result") UserAnswerLinkQueryCommond qCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState
			) throws Exception {
		ObjectiveItems obi=userAnswerLinkService.findAllObjectiveByQnId(qCommond);
		PageInfo pi=new PageInfo();
		pi.setPageInfo(obi);
		List<PageInfo> result=new ArrayList<PageInfo>();
		result.add(pi);
		qCommond.setResultList(result);
		resultState.setMessage("查询成功");
		return "";
	}
	
	/**
	 * 统计---某一个问卷下所有的客观题
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-09-03
	 */
	@RequestMapping(value = "/getUserListByPnId")
	public String getUserListByPnId(
			@ModelAttribute("result") UserAnswerLinkQueryCommond qCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState
			) throws Exception {
		qCommond.setResultList(userAnswerLinkService.getUserListByPnId(qCommond));
		resultState.setMessage("查询成功");
		return "";
	}
	
	
	/**
	 * 统计---查询用户答过问卷信息 包括答案
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-09-04
	 */
	@RequestMapping(value = "/findAllQuestionByQnId")
	public String findAllQuestionByQnId(
			@ModelAttribute("result") UserAnswerLinkQueryCommond qCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState
			) throws Exception {
		ObjectiveItems obi=userAnswerLinkService.findAllQuestionByQnId(qCommond);
		PageInfo pi=new PageInfo();
		pi.setPageInfo(obi);
		List<PageInfo> result=new ArrayList<PageInfo>();
		result.add(pi);
		qCommond.setResultList(result);
		resultState.setMessage("查询成功");
		return "";
	}
	
	/**
	 * 提交试卷
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-09-09
	 */
	@RequestMapping(value = "/sumitPaper")
	public String submitPaper(@ModelAttribute("result") UserAnswerLinkQueryCommond qCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState) throws Exception{

		if(token.isTokenValid(request, true)){//判断token
			com.eorchis.module.user.domain.User user=(com.eorchis.module.user.domain.User)request.getSession().getAttribute(SessionConstant.USER_INFO);
			qCommond.setSearchUserId(user.getUserId());
			String studentAnswerInfoStr=qCommond.getSearchStudentAnswerInfoStr();
			
			//qCommond.setSearchUserId("40288068481f9a7401481fb33fb50001");
			//qCommond.setSearchQuestionnaireId("1");
			//String studentAnswerInfoStr="[{\"itemId\":\"多选1\",\"studentResult\":[\"B\",\"C\"],\"itemType\":\"duoxt\"},{\"itemId\":\"单选1\",\"studentResult\":[\"B\"],\"itemType\":\"danxt\"},{\"itemId\":\"问答1\",\"studentResult\":[\"课程讲的很细,学习到很多东西\"],\"itemType\":\"wendt\"},{\"itemId\":\"单选2\",\"studentResult\":[],\"itemType\":\"danxt\"}]";
			List<StudentAnswerSelect> studentAnswerList = StudentPageInfoUtils.str2StuAnswerList(studentAnswerInfoStr);
			qCommond.setStudentAnswerSelect(studentAnswerList);
			userAnswerLinkService.addUserAnswerLink(qCommond);
			QueryClassUserBean qc=new QueryClassUserBean();
			qc.setClassId(qCommond.getSearchClassId());
			qc.setUserId(user.getUserId());
			qc.setUserName(user.getUserName());
			if(!classUserService.existenceClassUser(qc))
				classUserService.addAnonymous(qc);
			resultState.setMessage("");
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
			return "";
		}else{
			resultState.setMessage("不允许重复提交！");
			resultState.setState(ResultState.RESULT_STATE_FAIL);
			return "";
		}
	}
	/**
	 * 暂存试卷
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-11-05
	 */
	@RequestMapping(value = "/addUserAnswerLinkTemp")
	public String addUserAnswerLinkTemp(@ModelAttribute("result") UserAnswerLinkQueryCommond qCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState) throws Exception
	{
		com.eorchis.module.user.domain.User user=(com.eorchis.module.user.domain.User)request.getSession().getAttribute(SessionConstant.USER_INFO);
		qCommond.setSearchUserId(user.getUserId());
		
		String studentAnswerInfoStr=qCommond.getSearchStudentAnswerInfoStr();
		List<StudentAnswerSelect> studentAnswerList = StudentPageInfoUtils.str2StuAnswerList(studentAnswerInfoStr);
		qCommond.setStudentAnswerSelect(studentAnswerList);
		userAnswerLinkService.addUserAnswerLinkTemp(qCommond);
		resultState.setMessage("");
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}

	/**
	 * 导出评估结果统计汇总表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author LongJZ
	 * @createDate 2016-2-24
	 */
	@RequestMapping(value="/exportAssessResult")
	public void exportAssessResult(@ModelAttribute("resultList") UserAnswerLinkQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState) throws Exception {
		StatisticsHeaderBean headerBean = new StatisticsHeaderBean();
		List<ScoreStatisticsBean> scoreList = new ArrayList<ScoreStatisticsBean>();
		List<RadioStatisticsBean> radioList = new ArrayList<RadioStatisticsBean>();
		List<MCQStatisticsBean> MCQList = new ArrayList<MCQStatisticsBean>();
		List<QuestionsStatisticsBean> questionsList = new ArrayList<QuestionsStatisticsBean>();
		if (PropertyUtil.objectNotEmpty(queryCommond.getSearchQuestionnaireId())
				&& PropertyUtil.objectNotEmpty(queryCommond.getSearchClassId())) {
			headerBean = userAnswerLinkService.findHeader(queryCommond); // 表头信息
			scoreList = userAnswerLinkService.findScoreRadioList(queryCommond); // 数值型单选题统计集合
			radioList = userAnswerLinkService.findTextRadioList(queryCommond); // 文字型单选题统计集合
			MCQList = userAnswerLinkService.findMCQList(queryCommond); // 多选题统计集合
			questionsList = userAnswerLinkService.findQuestionsList(queryCommond); // 问答题统计集合
		}
		if (!PropertyUtil.objectNotEmpty(headerBean))
			return;
		try {
			HSSFWorkbook wb = new HSSFWorkbook();
			HSSFSheet sheet = wb.createSheet("汇总");
			HSSFCellStyle style1 = wb.createCellStyle(); // 样式1 标宋22粗体 水平垂直居中
			style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
			style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
			// 创建字体样式
		    HSSFFont font1 = wb.createFont();
		    font1.setFontName("方正小标宋简体");
		    font1.setBoldweight((short) 100);
		    font1.setFontHeight((short) 440);
		    style1.setFont(font1);
			
			HSSFCellStyle style2 = wb.createCellStyle(); // 样式2 黑体12粗体 水平垂直居中 细黑边框
			style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
			style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
		    style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		    style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		    style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
		    style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
			// 创建字体样式
		    HSSFFont font2 = wb.createFont();
		    font2.setFontName("黑体");
		    font2.setBoldweight((short) 60);
		    font2.setFontHeight((short) 240);
		    style2.setFont(font2);

			HSSFCellStyle style3 = wb.createCellStyle(); // 样式3 宋体12 水平垂直居中 细黑边框
			style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
			style3.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
		    style3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		    style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		    style3.setBorderRight(HSSFCellStyle.BORDER_THIN);
		    style3.setBorderTop(HSSFCellStyle.BORDER_THIN);
			// 创建字体样式
		    HSSFFont font3 = wb.createFont();
		    font3.setFontName("宋体");
//		    font3.setBoldweight((short) 60);
		    font3.setFontHeight((short) 240);
		    style3.setFont(font3);

			HSSFCellStyle style4 = wb.createCellStyle(); // 样式4 宋体12 左对齐 细黑边框
			style4.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
			style4.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 左对齐
		    style4.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		    style4.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		    style4.setBorderRight(HSSFCellStyle.BORDER_THIN);
		    style4.setBorderTop(HSSFCellStyle.BORDER_THIN);
		    style4.setFont(font3);

			HSSFCellStyle style5 = wb.createCellStyle(); // 样式5 宋体12 右对齐 细黑边框
			style5.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
			style5.setAlignment(HSSFCellStyle.ALIGN_RIGHT); // 右对齐
		    style5.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		    style5.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		    style5.setBorderRight(HSSFCellStyle.BORDER_THIN);
		    style5.setBorderTop(HSSFCellStyle.BORDER_THIN);
		    style5.setFont(font3);
		    
			HSSFRow row = sheet.createRow(0);// 表格第一行
			sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 8)); // 合并9列
			HSSFCell ce = row.createCell(0);
			ce.setCellValue(headerBean.getClassName()); // 班级名称
			ce.setCellStyle(style1); // 样式，居中
		    
			HSSFRow row2 = sheet.createRow(1);// 表格第二行
			row2.setHeight((short) 500);
			HSSFCell cell = row2.createCell(0);
			cell.setCellValue("参加评估人数");
			cell.setCellStyle(style2);
			cell = row2.createCell(1);
			cell.setCellValue(headerBean.getAnswerUserCount());
			cell.setCellStyle(style2);
		    if (!PropertyUtil.objectNotEmpty(scoreList)) {
				for (int i = 2; i < 9; i++){
					cell = row2.createCell(i);
					cell.setCellValue(""); // 空单元格
					cell.setCellStyle(style4); // 样式，左对齐
				}
				sheet.addMergedRegion(new CellRangeAddress(1, 1, 2, 8));
		    } else {
				cell = row2.createCell(2);
				cell.setCellValue("单项总分");
				cell.setCellStyle(style2);
				cell = row2.createCell(3);
				cell.setCellValue("单项平均分");
				cell.setCellStyle(style2);
				cell = row2.createCell(4);
				cell.setCellValue("类总分");
				cell.setCellStyle(style2);
				cell = row2.createCell(5);
				cell.setCellValue("类单项平均分");
				cell.setCellStyle(style2);
				cell = row2.createCell(6);
				cell.setCellValue("总项总分");
				cell.setCellStyle(style2);
				cell = row2.createCell(7);
				cell.setCellValue("总项平均分");
				cell.setCellStyle(style2);
				cell = row2.createCell(8);
				cell.setCellValue("总体平均分");
				cell.setCellStyle(style2);

				String lastTestleName = "";
				List<Integer> testletCount = new ArrayList<Integer>();
				for (int i = 0; i < scoreList.size(); i++) {
					if (!lastTestleName.equals(scoreList.get(i).getTestletName()))
						testletCount.add(scoreList.get(i).getTestletCount());
					HSSFRow row3 = sheet.createRow(i + 2);
					HSSFCell cell2 = null;
					cell2 = row3.createCell(0);
					cell2.setCellValue(scoreList.get(i).getTestletName()); // 题组名称
//					sheet.setColumnWidth(0, scoreList.get(i).getTestletName().getBytes().length * 256);
					lastTestleName = scoreList.get(i).getTestletName();
					cell2.setCellStyle(style3); // 样式，居中

					cell2 = row3.createCell(1);
					cell2.setCellValue(scoreList.get(i).getQuestionName()); // 题干
					cell2.setCellStyle(style4); // 样式，左对齐

					cell2 = row3.createCell(2);
					cell2.setCellValue(scoreList.get(i).getUnitermSum()); // 单项总分
					cell2.setCellStyle(style5); // 样式，右对齐

					cell2 = row3.createCell(3);
					cell2.setCellValue(scoreList.get(i).getUnitermAvg()); // 单项平均分
					cell2.setCellStyle(style5); // 样式，右对齐

					cell2 = row3.createCell(4);
					cell2.setCellValue(scoreList.get(i).getTestletSum()); // 类总分
					cell2.setCellStyle(style3); // 样式，居中

					cell2 = row3.createCell(5);
					cell2.setCellValue(scoreList.get(i).getTestletAvg()); // 类单项平均分
					cell2.setCellStyle(style3); // 样式，居中

					cell2 = row3.createCell(6);
					cell2.setCellValue(scoreList.get(i).getTotalSum()); // 总项总分
					cell2.setCellStyle(style3); // 样式，居中

					cell2 = row3.createCell(7);
					cell2.setCellValue(scoreList.get(i).getTotalAvg()); // 总项平均分
					cell2.setCellStyle(style3); // 样式，居中

					cell2 = row3.createCell(8);
					cell2.setCellValue(scoreList.get(i).getAllAvg()); // 总体平均分
					cell2.setCellStyle(style3); // 样式，居中
				}
				int rowNum = 2;
				for (int i = 0; i < testletCount.size(); i++){
					if(PropertyUtil.objectNotEmpty(testletCount.get(i))){
						sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum + testletCount.get(i) - 1, 0, 0));
						sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum + testletCount.get(i) - 1, 4, 4));
						sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum + testletCount.get(i) - 1, 5, 5));
						rowNum = rowNum + testletCount.get(i);
					}
				}
				sheet.addMergedRegion(new CellRangeAddress(2, scoreList.size() + 1, 6, 6));
				sheet.addMergedRegion(new CellRangeAddress(2, scoreList.size() + 1, 7, 7));
				sheet.addMergedRegion(new CellRangeAddress(2, scoreList.size() + 1, 8, 8));
				
			}

		    if (PropertyUtil.objectNotEmpty(radioList)) {
				String questionId = "";
				List<Integer> radioCount = new ArrayList<Integer>();
				String lastTestleName = "";
				List<Integer> testletCount = new ArrayList<Integer>();
				for (int i = 0; i < radioList.size(); i++) {
					if (!questionId.equals(radioList.get(i).getQuestionId()))
						radioCount.add(radioList.get(i).getOptionCount());
					if (!lastTestleName.equals(radioList.get(i).getTestletName()))
						testletCount.add(radioList.get(i).getTestletCount());
					HSSFRow row3 = sheet.createRow(sheet.getLastRowNum() + 1);
					HSSFCell cell2 = null;
					cell2 = row3.createCell(0);
					cell2.setCellValue(radioList.get(i).getTestletName()); // 题组名称
					lastTestleName = radioList.get(i).getTestletName();
					cell2.setCellStyle(style3); // 样式，居中
					
					cell2 = row3.createCell(1);
					cell2.setCellValue(radioList.get(i).getQuestionName()); // 题干
					questionId = radioList.get(i).getQuestionId();
					cell2.setCellStyle(style4); // 样式，居中

					cell2 = row3.createCell(2);
					cell2.setCellValue(radioList.get(i).getOptionName()); // 选项
					cell2.setCellStyle(style4); // 样式，左对齐

					cell2 = row3.createCell(3);
					cell2.setCellValue(radioList.get(i).getSelectedNum()); // 选择人数
					cell2.setCellStyle(style5); // 样式，右对齐
					
					for (int j = 4; j < 9; j++){
						cell2 = row3.createCell(j);
						cell2.setCellValue(""); // 空单元格
						cell2.setCellStyle(style4); // 样式，左对齐
					}
					sheet.addMergedRegion(new CellRangeAddress(row3.getRowNum(), row3.getRowNum(), 4, 8));
				}
				int rowNum = 2;
				if (PropertyUtil.objectNotEmpty(scoreList))
					rowNum += scoreList.size();
				int rowNum2 = rowNum;
				for (int i = 0; i < testletCount.size(); i++){
					sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum + testletCount.get(i) - 1, 0, 0));
					rowNum += testletCount.get(i);
				}
				for (int j = 0; j < radioCount.size(); j++){
					sheet.addMergedRegion(new CellRangeAddress(rowNum2, rowNum2 + radioCount.get(j) - 1, 1, 1));
					rowNum2 += radioCount.get(j);
				}
		    }
			
		    if (PropertyUtil.objectNotEmpty(MCQList)) {
				String questionId = "";
				List<Integer> MCQCount = new ArrayList<Integer>();
				for (int i = 0; i < MCQList.size(); i++) {
					if (!questionId.equals(MCQList.get(i).getQuestionId()))
						MCQCount.add(MCQList.get(i).getOptionCount());
					HSSFRow row3 = sheet.createRow(sheet.getLastRowNum() + 1);
					HSSFCell cell2 = null;
					cell2 = row3.createCell(0);
					cell2.setCellValue(MCQList.get(i).getQuestionName()); // 题干
					questionId = MCQList.get(i).getQuestionId();
					cell2.setCellStyle(style3); // 样式，居中

					cell2 = row3.createCell(1);
					cell2.setCellValue(MCQList.get(i).getOptionName()); // 选项
					cell2.setCellStyle(style4); // 样式，左对齐

					cell2 = row3.createCell(2);
					cell2.setCellValue(MCQList.get(i).getSelectedNum()); // 选择人数
					cell2.setCellStyle(style5); // 样式，右对齐

					cell2 = row3.createCell(3);
					cell2.setCellValue(MCQList.get(i).getQuestionAnswer()); // 主观回答
					cell2.setCellStyle(style4); // 样式，左对齐
					
					for (int j = 4; j < 9; j++){
						cell2 = row3.createCell(j);
						cell2.setCellValue(""); // 空单元格
						cell2.setCellStyle(style4); // 样式，左对齐
					}
					
					sheet.addMergedRegion(new CellRangeAddress(row3.getRowNum(), row3.getRowNum(), 3, 8));
				}
				int rowNum = 2;
				if (PropertyUtil.objectNotEmpty(scoreList))
					rowNum += scoreList.size();
				if (PropertyUtil.objectNotEmpty(radioList))
					rowNum += radioList.size();
				for (int i = 0; i < MCQCount.size(); i++){
					sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum + MCQCount.get(i) - 1, 0, 0));
					rowNum = rowNum + MCQCount.get(i);
				}
		    }

		    if (PropertyUtil.objectNotEmpty(questionsList)) {
				for (int i = 0; i < questionsList.size(); i++) {
					HSSFRow row3 = sheet.createRow(sheet.getLastRowNum() + 1);
					HSSFCell cell2 = null;
					cell2 = row3.createCell(0);
					cell2.setCellValue(questionsList.get(i).getQuestionName()); // 题干
					cell2.setCellStyle(style4); // 样式，左对齐
					
					cell2 = row3.createCell(1);
					cell2.setCellValue(""); // 空格
					cell2.setCellStyle(style4); // 样式，左对齐

					cell2 = row3.createCell(2);
					cell2.setCellValue(questionsList.get(i).getAnswerUserNum()); // 人数
					cell2.setCellStyle(style5); // 样式，右对齐
					
					for (int j = 3; j < 9; j++){
						cell2 = row3.createCell(j);
						cell2.setCellValue(""); // 空单元格
						cell2.setCellStyle(style4); // 样式，左对齐
					}

					sheet.addMergedRegion(new CellRangeAddress(row3.getRowNum(), row3.getRowNum(), 0, 1));
					sheet.addMergedRegion(new CellRangeAddress(row3.getRowNum(), row3.getRowNum(), 3, 8));
				}
		    }
		    
		    Iterator<Row> everyRow = sheet.rowIterator();
		    List<Integer> firstCellSize = new ArrayList<Integer>();
		    while(everyRow.hasNext()) {
		    	Row arow = everyRow.next();
		    	if (arow.getRowNum() != 0)
		    		firstCellSize.add(arow.getCell(0).getStringCellValue().length());
		    }
			sheet.setColumnWidth(0, 5*3* 256);
			sheet.setColumnWidth(1, 8*3* 256);
			sheet.setColumnWidth(2, 10 * 256);
			sheet.setColumnWidth(3, 12 * 256);
			sheet.setColumnWidth(4, 10 * 256);
			sheet.setColumnWidth(5, 14 * 256);
			sheet.setColumnWidth(6, 10 * 256);
			sheet.setColumnWidth(7, 12 * 256);
			sheet.setColumnWidth(8, 12 * 256);

			/*for (int i = 0; i < 9; i++) {
				sheet.autoSizeColumn(i, true);
			}*/
			
			response.reset();
			response.setContentType("application/x-msdownload");
			response.setHeader("Content-Disposition","attachment; filename="
					+ new String((headerBean.getClassName()+"-评估结果统计汇总表").getBytes("gb2312"),"ISO-8859-1") + ".xls");
			ServletOutputStream outStream = null;
			
			try {
				outStream = response.getOutputStream();
				wb.write(outStream);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				outStream.close();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public static void main(String [] args) {
		try {
			String studentAnswerInfoStr="[{\"itemId\":\"100\",\"studentResult\":[\"B\",\"C\"],\"itemType\":\"duoxt\"},{\"itemId\":\"101\",\"studentResult\":[\"B\"],\"itemType\":\"duoxt\"},{\"itemId\":\"102\",\"studentResult\":[\"课程讲的很细,学习到很多东西\"],\"itemType\":\"duoxt\"},{\"itemId\":\"103\",\"studentResult\":[],\"itemType\":\"duoxt\"}]";
			List<StudentAnswerSelect> studentAnswerList;
			studentAnswerList = StudentPageInfoUtils.str2StuAnswerList(studentAnswerInfoStr);
			for(int i=0;i<studentAnswerList.size();i++) {
				StudentAnswerSelect sas=studentAnswerList.get(i);
				System.out.print(sas.getItemId());
				System.out.println("   "+sas.getItemType());
				for(int j=0;j<sas.getStudentResult().size();j++) {
					System.out.println(sas.getStudentResult().get(j));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
